﻿#include "FingerModule/FingerWgt.h"
#include "ui_FingerWgt.h"

#include <QDebug>

FingerWgt::FingerWgt(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FingerWgt)
{
    ui->setupUi(this);
#if 0
    QString info;
    m_finger = new FingerModule;
    if(!m_finger->openSerialPort(57600, info)){
        qDebug() << info;
    }
    connect(m_finger, &FingerModule::sigShowMsg, this, &FingerWgt::onShowMsg, Qt::QueuedConnection);
    connect(this, &FingerWgt::sigSendCtrlCmd, m_finger, &FingerModule::onSendCtrlCmd, Qt::QueuedConnection);
    connect(m_finger, &FingerModule::sigReadConListOver, this, &FingerWgt::onUpdateConList, Qt::QueuedConnection);
    connect(m_finger, &FingerModule::sigUpImageOver, this, [this](){
        QImage imageScale = m_finger->m_fingerImg.scaled(QSize(ui->imageLab->width(), ui->imageLab->height()));
        QPixmap pixmap = QPixmap::fromImage(imageScale);
        ui->imageLab->setPixmap(pixmap);
        //m_finger->m_fingerImg.save("./finger.bmp");
    });

    //自动登记模式的异步信号处理.
    connect(m_finger, &FingerModule::sigAutoLoginOver, this, [this](bool flag, int tempId, QString info){
        if(flag){
            int row = tempId / 10;
            int col = tempId % 10;
            QTableWidgetItem *item = ui->tableWidget->item(row, col);
            if(nullptr == item){
                item = new QTableWidgetItem;
                ui->tableWidget->setItem(row, col, item);
            }
            item->setBackground(QBrush(QColor(Qt::green)));
        }
        emit sigUserLoginOver(flag, tempId, info);
    }, Qt::ConnectionType::QueuedConnection);

    //自动搜索模式的异步信号处理.
    connect(m_finger, &FingerModule::sigAutoSearchOver, this, [this](bool flag, int tempId, QString info){
        if(flag){
            QString text = QString::fromLocal8Bit("识别指纹%1").arg(tempId);
            emit sigPlayViocePrompt(text);

            //数据库操作开始.
            emit sigUserSign(tempId); //用户签到.
        }
    }, Qt::ConnectionType::QueuedConnection);
    setupSignals();

    m_th = new QThread;
    m_finger->moveToThread(m_th);
    m_th->start(); //至此，线程登场.

    //发送信号，通知模块进入自动搜索模式.
    emit sigSendCtrlCmd(FingerCmd::PsCancel); //取消上次的状态.
    emit sigSendCtrlCmd(FingerCmd::ReadSysParam); //读系统参数.
    emit sigSendCtrlCmd(FingerCmd::ReadConList, 0); //读第0页指纹索引.
    emit sigSendCtrlCmd(FingerCmd::AutoSearch, 0, 0xFFFF, 01); //进入自动搜索状态.
#endif
    return;
}

FingerWgt::~FingerWgt()
{
    delete ui;
}

void FingerWgt::forceStop()
{
    if (m_finger) {
        m_finger->resetModule();
        m_finger->deleteLater();
    }
    if(m_th) m_th->quit();
    return;
}

void FingerWgt::onShowMsg(QString msg)
{
    ui->msgEdit->append(msg);
}

void FingerWgt::onUpdateConList()
{
    if(nullptr == m_finger) return;

    int totalCnt = m_finger->m_sysParam.m_totalTempCnt;
    int row = 0 == totalCnt % 10 ? totalCnt / 10 : totalCnt / 10 + 1 ;
    ui->tableWidget->setRowCount(row);
    ui->tableWidget->setColumnCount(10);
    for(int i=0; i<totalCnt; ++i){
        QTableWidgetItem *item = ui->tableWidget->item(i/10, i%10);
        if (nullptr == item) {
            item = new QTableWidgetItem;
            ui->tableWidget->setItem(i / 10, i % 10, item);
        }
        item->setText(QString::number(i, 16).toUpper());
        if(true == m_finger->m_conList[i]){
            item->setBackground(QBrush(QColor(Qt::green)));
        }
        else {
            item->setBackground(QBrush(QColor(Qt::white)));
        }
    }
    return;
}


void FingerWgt::onUserLogin()
{
    m_finger->resetModule(); //取消当前阻塞的槽函数.
    int tempId = m_finger->getFreeTempId(); //获取可用的指纹库ID
    QString info = QString::fromLocal8Bit("请按压手指");
    emit sigUserLoginOver(false, tempId, info);
    m_finger->resetModule(); //取消当前阻塞的槽函数.
    emit sigSendCtrlCmd(FingerCmd::PsCancel); //发指令取消当前操作.
    emit sigSendCtrlCmd(FingerCmd::AutoLogin, tempId, 2, 0x09);
    return;
}

void FingerWgt::onUserSearch()
{
    m_finger->resetModule(); //取消当前阻塞的槽函数.
    emit sigSendCtrlCmd(FingerCmd::PsCancel); //发指令取消当前操作.
    emit sigSendCtrlCmd(FingerCmd::AutoSearch, 0, 0xFFFF, 0x01);
    return;
}

void FingerWgt::onRemoveFinger(int fingerId)
{
    m_finger->resetModule(); //取消当前阻塞的槽函数.
    emit sigSendCtrlCmd(FingerCmd::PsCancel); //发指令取消当前操作.
    emit sigSendCtrlCmd(FingerCmd::DeleteChar, fingerId, 1);
    emit sigSendCtrlCmd(FingerCmd::AutoSearch, 0, 0xFFFF, 0x01);
    return;
}

void FingerWgt::setupSignals()
{
    connect(ui->vfyPwdBtn, &QPushButton::clicked, this, [this](){
        int pwd = ui->vfyPwdEdit_1->text().toUInt(nullptr, 16);
        emit sigSendCtrlCmd(FingerCmd::VerifyPassword, pwd);
    });
    connect(ui->getEchoBtn, &QPushButton::clicked, this, [this](){
        emit sigSendCtrlCmd(FingerCmd::GetEcho);
    });
    connect(ui->genImgBtn, &QPushButton::clicked, this, [this](){
        emit sigSendCtrlCmd(FingerCmd::GenImg);
    });
    connect(ui->upImageBtn, &QPushButton::clicked, this, [this](){
        emit sigSendCtrlCmd(FingerCmd::UpImage);
        //m_finger->onSendCtrlCmd(FingerCmd::UpImage); //不能在GUI线程中访问另一个线程下的Object
    });
    connect(ui->img2TzBtn, &QPushButton::clicked, this, [this](){
        int bufNo = ui->img2TzEdit_1->text().toUInt(nullptr, 16);
        emit sigSendCtrlCmd(FingerCmd::Img2Tz, bufNo);
    });
    connect(ui->regModelBtn, &QPushButton::clicked, this, [this](){
        emit sigSendCtrlCmd(FingerCmd::RegModel);
    });
    connect(ui->storeBtn, &QPushButton::clicked, this, [this](){
        int bufNo = ui->storeEdit_1->text().toUInt(nullptr, 16);
        int tempId = ui->storeEdit_2->text().toUInt(nullptr, 16);
        emit sigSendCtrlCmd(FingerCmd::Store, bufNo, tempId);
    });
    connect(ui->deleteCharBtn, &QPushButton::clicked, this, [this](){
        int tempId = ui->deleteCharEdit_1->text().toUInt(nullptr, 16);
        int tempCnt = ui->deleteCharEdit_2->text().toUInt(nullptr, 16);
        emit sigSendCtrlCmd(FingerCmd::DeleteChar, tempId, tempCnt);
    });
    connect(ui->searchBtn, &QPushButton::clicked, this, [this](){
        int bufNo = ui->searchEdit_1->text().toUInt(nullptr, 16);
        int tempId = ui->searchEdit_2->text().toUInt(nullptr, 16);
        int count = ui->searchEdit_3->text().toUInt(nullptr, 16);
        emit sigSendCtrlCmd(FingerCmd::Search, bufNo, tempId, count);
    });
    connect(ui->readSysParamBtn, &QPushButton::clicked, this, [this](){
        emit sigSendCtrlCmd(FingerCmd::ReadSysParam);
    });
    connect(ui->templateNumBtn, &QPushButton::clicked, this, [this](){
        emit sigSendCtrlCmd(FingerCmd::TemplateNum);
    });
    connect(ui->readConListBtn, &QPushButton::clicked, this, [this](){
        int index = ui->readConListEdit_1->text().toUInt(nullptr, 16);
        emit sigSendCtrlCmd(FingerCmd::ReadConList, index);
    });
    connect(ui->emptyBtn, &QPushButton::clicked, this, [this](){
		emit sigSendCtrlCmd(FingerCmd::Empty);
	});
    connect(ui->autoLoginBtn, &QPushButton::clicked, this, [this](){
        int tempId = ui->autoLoginEdit_1->text().toUInt(nullptr, 16);
        int count = ui->autoLoginEdit_2->text().toUInt(nullptr, 16);
        int flag = ui->autoLoginEdit_3->text().toUInt(nullptr, 16);
        emit sigSendCtrlCmd(FingerCmd::AutoLogin, tempId, count, flag);
    });
    connect(ui->autoSearchBtn, &QPushButton::clicked, this, [this](){
        int rank = ui->autoSearchEdit_1->text().toUInt(nullptr, 16);
        int tempId = ui->autoSearchEdit_2->text().toUInt(nullptr, 16);
        int flag = ui->autoSearchEdit_3->text().toUInt(nullptr, 16);
        emit sigSendCtrlCmd(FingerCmd::AutoSearch, rank, tempId, flag);
    });
    connect(ui->cancelBtn, &QPushButton::clicked, this, [this](){
        m_finger->resetModule();
        emit sigSendCtrlCmd(FingerCmd::PsCancel);
    });
    connect(ui->clearMsgBtn, &QPushButton::clicked, this, [this](){
        ui->msgEdit->clear();
    });
    return;
}





